package com.example.myapp1211;

import android.os.Bundle;

import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapp1211.Fragments.ChatFragment;
import com.example.myapp1211.Fragments.ConfigFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private TextView titleText;
    private Toolbar toolbar;
    private FragmentTransaction fragmentTransaction;
    private ChatFragment chatFragment=new ChatFragment();
    private ConfigFragment configFragment=new ConfigFragment();
    private BottomNavigationView.OnNavigationItemSelectedListener
            mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            toolbar.setVisibility(View.VISIBLE);
            fragmentTransaction = getSupportFragmentManager().beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_chat:
                    hideAllFragement();
                    fragmentTransaction.show(chatFragment);
                    fragmentTransaction.commit();
                    titleText.setText(R.string.title_chat);
                    return true;
                case R.id.navigation_home:
                    hideAllFragement();
                    titleText.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    hideAllFragement();
                    titleText.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    toolbar.setVisibility(View.GONE);
                    hideAllFragement();
                    fragmentTransaction.show(configFragment);
                    fragmentTransaction.commit();
                    titleText.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        hideActionBar();
        addFragment();
    }
    private void initView(){
        BottomNavigationView navView = findViewById(R.id.nav_view);
        toolbar=findViewById(R.id.toolbar);
        titleText=findViewById(R.id.toolbartlitle);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }
    private void hideActionBar(){
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }
    private void hideAllFragement(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(chatFragment != null){
            transaction.hide(chatFragment);
        }
        if(configFragment != null){
            transaction.hide(configFragment);
        }
        transaction.commit();
    }
    private void addFragment(){
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,chatFragment);
        fragmentTransaction.add(R.id.fragment_container,configFragment);
        hideAllFragement();
        fragmentTransaction.show(chatFragment);
        fragmentTransaction.commit();
    }
}

